package com.chwn.harulib.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.chwn.harulib.util.LogUtil;

/**
 * Created by Myshikin on 2017/3/20.
 */

public class SwipeBackActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isDisableDrag()) {
            setupDragView();
        }
    }

    public void setupDragView() {
        if (mDrawerLayout != null) {
            return;
        }
        mDrawerLayout = new DrawerLayout(this);
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        ViewGroup linearParent = (ViewGroup) decorView.getChildAt(0);

        decorView.removeView(linearParent);
        decorView.addView(
                mDrawerLayout, new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                )
        );
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        mDrawerLayout.addView(linearParent, 0);
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.e("myshikin", "onDrawerOpened");
                mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                LogUtil.e("onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.e("myshikin", "onDrawerClosed");
                mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                LogUtil.e("onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    protected boolean isDisableDrag() {
        return true;
    }

}
